﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DataGridViewAutoFilter;
using DocPlat.Library;

namespace DocPlat.UI
{
    public partial class Payer : Form
    {
        ServerObject mServerObject = null;
        bool mCloseAfterSave = false;
        bool mNewRecord = false;
        string mSelectedIDNO = string.Empty;
        string mOldIDNO = string.Empty;
        string mSelectedFirstName = string.Empty;
        string mSelectedLastName = string.Empty;
        string mSelectedPatronimic = string.Empty;

        public bool NewRecord
        {
            get { return mNewRecord; }
            set { mNewRecord = value; }
        }

        public bool CloseAfterSave
        {
            get { return mCloseAfterSave; }
            set { mCloseAfterSave = value; }
        }

        public string SelectedIDNO
        {
            get { return mSelectedIDNO; }
            set { mSelectedIDNO = value; }
        }

        public string SelectedFirstName
        {
            get { return mSelectedFirstName; }
            set { mSelectedFirstName = value; }
        }

        public string SelectedLastName
        {
            get { return mSelectedLastName; }
            set { mSelectedLastName = value; }
        }

        public string SelectedPatronimic
        {
            get { return mSelectedPatronimic; }
            set { mSelectedPatronimic = value; }
        }


        public Payer()
        {
            InitializeComponent();
            mServerObject = new ServerObject();
        }

        private void FillPayerGridView()
        {
            DataTable sourceTable = mServerObject.GetPayerList();
            BindingSource bindigSource = new BindingSource(sourceTable, null);
            payerGridView.DataSource = bindigSource;

            if (payerGridView.DataSource != null)
            {
                foreach (DataGridViewColumn col in payerGridView.Columns)
                {
                    col.HeaderCell = new DataGridViewAutoFilterColumnHeaderCell(col.HeaderCell);
                }

                payerGridView.AutoResizeColumns();
            }

            if (sourceTable == null || sourceTable.Rows.Count == 0)
            { mNewRecord = true; }
        }

        private void Payer_Load(object sender, EventArgs e)
        {
            FillPayerGridView();
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            mOldIDNO = string.Empty;
            mSelectedFirstName = string.Empty;
            mSelectedLastName = string.Empty;
            mSelectedPatronimic = string.Empty;

            this.DialogResult = DialogResult.Cancel;
        }

        private bool isComplete()
        {
            bool result = false;

            dataValidator dv = new dataValidator();

            if (dv.IsNumber(idnoTextBox.Text) && idnoTextBox.Text.Length == 13)
            {
                idnoTextBox.BackColor = Color.White;
                result = true;
            }
            else
            {
                idnoTextBox.BackColor = Color.Aqua;
            }

            return result;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            if (isComplete())
            {
                if (mNewRecord)
                {
                    if (!mSelectedIDNO.Equals(string.Empty) && mServerObject.AddPayer(mSelectedIDNO, mSelectedFirstName, mSelectedLastName, mSelectedPatronimic))
                    { }
                    else
                    { MessageBox.Show("Error saving new record!!!", "Attention, error!!!"); }
                }
                else
                {
                    if (!mSelectedIDNO.Equals(string.Empty) && mServerObject.UpdatePayer(mSelectedIDNO, mSelectedFirstName, mSelectedLastName, mSelectedPatronimic, mOldIDNO))
                    { }
                    else
                    { MessageBox.Show("Error updating record!!!", "Attention, error!!!"); }
                }

                if (mCloseAfterSave) this.DialogResult = DialogResult.OK;
                else
                {
                    firstNameTextBox.Text = string.Empty;
                    lastNameTextBox.Text = string.Empty;
                    patronimicTextBox.Text = string.Empty;
                    idnoTextBox.Text = string.Empty;
                    mNewRecord = true;
                    FillPayerGridView();
                }
            }
        }

        private void newPayerButton_Click(object sender, EventArgs e)
        {
            firstNameTextBox.Text = string.Empty;
            lastNameTextBox.Text = string.Empty;
            patronimicTextBox.Text = string.Empty;
            idnoTextBox.Text = string.Empty;
            mNewRecord = true;
        }

        private void payerGridView_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            if (payerGridView.SelectedRows.Count == 1)
            {
                idnoTextBox.Text = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["IDNO"].Value.ToString();
                firstNameTextBox.Text = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["FirstName"].Value.ToString();
                lastNameTextBox.Text = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["LastName"].Value.ToString();
                patronimicTextBox.Text = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["Patronimic"].Value.ToString();

                mSelectedIDNO = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["IDNO"].Value.ToString();
                mOldIDNO = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["IDNO"].Value.ToString();
                mSelectedFirstName = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["FirstName"].Value.ToString();
                mSelectedLastName = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["LastName"].Value.ToString();
                mSelectedPatronimic = payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["Patronimic"].Value.ToString();

                mNewRecord = false;
            }
        }

        private void firstNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mSelectedFirstName = firstNameTextBox.Text.Trim();
        }

        private void lastNameTextBox_TextChanged(object sender, EventArgs e)
        {
            mSelectedLastName = lastNameTextBox.Text.Trim();
        }

        private void patronimicTextBox_TextChanged(object sender, EventArgs e)
        {
            mSelectedPatronimic = patronimicTextBox.Text.Trim();
        }

        private void idnoTextBox_TextChanged(object sender, EventArgs e)
        {     
            dataValidator dv = new dataValidator();

            if (dv.IsNumber(idnoTextBox.Text) && idnoTextBox.Text.Length == 13)
            {
                mSelectedIDNO = idnoTextBox.Text;                
                idnoTextBox.BackColor = Color.White;
            }
            else
            {
                idnoTextBox.BackColor = Color.Aqua;
            }
        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Are you sure ???", "Attention!", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                if (payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["IDNO"].Value.ToString().Equals(string.Empty) 
                    || !mServerObject.DeletePayer(payerGridView.Rows[payerGridView.SelectedRows[0].Index].Cells["IDNO"].Value.ToString()))
                {
                    MessageBox.Show("Error deleting record!");
                }

                FillPayerGridView();

                firstNameTextBox.Text = string.Empty;
                lastNameTextBox.Text = string.Empty;
                patronimicTextBox.Text = string.Empty;
                idnoTextBox.Text = string.Empty;
            }
        }


    }
}
